﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using Frogism.Core;
using System.Data;
using System.Data.SqlClient;

namespace Frogism.Data {
  public class TransactionLoader : LoaderUpdateBase<Transaction, ITransaction> {

    protected override ITransaction GetEmptyObject() {
      return new Transaction();
    }
    const int MISC_CATEGORY_ID = 11;
    protected override void FillObject(ITransaction transaction,FriendlyReader reader) {

      transaction.AccountId = reader.Int("AccountId");
      transaction.Amount = reader.Decimal("Amount");
      transaction.Id = reader.Int("Id");
      transaction.IsConfirmed = reader.Bool("IsConfirmed");
      transaction.Note = reader.String("Note");
      transaction.TransactionDate = reader.Date("Date");
      ObjectNameBaseLoader categoryLoader = new ObjectNameBaseLoader();
      if(!reader.IsNull("CategoryId")) {
        transaction.Category = categoryLoader.GetById("Category", reader.Int("CategoryId"));
      }
      transaction.TransactionType = TransactionType.Create(reader.Int("TransactionTypeId"));
    }
    public Collection<ITransaction> TransactionByAccount(int accountId) {
      return this.ProcedureGetAll("TransactionGetByAccount", new IntegerParameter() { Name = "AccountId", Value = accountId });
    }

    protected override void FillParameters(SqlCommand cmd, ITransaction transaction) {
        AddStringParam(cmd, "Note", transaction.Note);
        AddIntParam(cmd, "AccountId", transaction.AccountId);
        AddIntParam(cmd, "TransactionTypeId", transaction.TransactionType.Value);
        AddDecimalParam(cmd, "Amount", transaction.Amount);
        AddDateParam(cmd, "Date", transaction.TransactionDate);
        AddBoolParam(cmd, "IsConfirmed", transaction.IsConfirmed);
        AddIntParam(cmd, "CategoryId", transaction.Category.Id);
    }



    protected override string ProcNameGetAll {
      get { return "TransactionGetAll"; }
    }

    protected override string ProcNameGetById {
      get { return "TransactionGetById"; }
    }
    protected override string ProcNameSave {
      get { return "TransactionUpdate"; }
    }
    protected override string ProcNameCreate {
      get { return "TransactionInsert"; }
    }
    protected override string ProcNameDelete {
      get { return "TransactionDelete"; }
    }
  }
}
